package pers.wk.queues.work;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import pers.wk.config.MQConfig;
import pers.wk.utils.MQConnectionUtils;

/**
 * 工作队列-生产者
 */
public class Producer {

    private static String queueName = MQConfig.QueueNames.WORK_QUEUE_NAME;

    public static void main(String[] args) {
        //1.ConnectionFactory
        ConnectionFactory connectionFactory = MQConnectionUtils.getConnectionFactory("/");
        Connection connection = null;
        Channel channel = null;
        try {
            //2.创建连接
            connection = connectionFactory.newConnection();
            //3.通过连接获取通道Channel
            channel = connection.createChannel();
            //创建队列声明
            channel.queueDeclare(queueName,
                    false, false, false, null);
            System.out.println("工作队列-生产者，启动。。。");

            for (int i = 1; i <= 10; i++) {
                String msg = "hello world";
                msg = msg + i;
                System.out.println("工作队列-生产者发送消息：" + msg);
                channel.basicPublish("", queueName, null, msg.getBytes());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MQConnectionUtils.close(connection, channel);
        }
    }
}
